package zero.tech.games.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import zero.tech.core.util.RandomUtil;
import zero.tech.redis.cache.RedisUtil;

@Component
public class RoomIdUtil {
	@Autowired
	private RedisUtil redisUtil;
	public String randRoomId() {
		int size = 6;
		String num = RandomUtil.randNum(size);
		int count = 0;
		boolean flag = true;
		while (flag) {
			flag = redisUtil.hexists(RedisConsts.B_ROOMIDS, String.valueOf(num));
			if (!flag) {
				redisUtil.hset(RedisConsts.B_ROOMIDS, String.valueOf(num), String.valueOf(num));
			} else {
				num = RandomUtil.randNum(size);
				count += 1;
			}
			if (count > 2) {
				size++;
			}
		}
		return num;
	}
	public void deleteRoomId(String room_id) {
		redisUtil.hdel(RedisConsts.B_ROOMIDS, room_id);
	}
	public void deleteRoomIdBatch(Object... room_ids) {
		redisUtil.hdel(RedisConsts.B_ROOMIDS, room_ids);
	}
}
